package FlowGraph.Nodes;

import java.util.*;

import antlr.collections.AST;

import FlowGraph.FlowGraphNode;
import FlowGraph.StatementInfo.*;

//////////////////////////////////////////////////////////////////////////
//// BasicBlockNode
/**

@author Ivan Jeukens
@version v 0.1 03/10/2001
*/
public class BasicBlockNode extends FlowGraphNode {

    public BasicBlockNode() {
        super(null, BASIC_BLOCK);
        _statements = new LinkedList();
        _statementsInfo = new Vector();
    }

    public BasicBlockNode(AST node) {
        super(null, BASIC_BLOCK);
        _statements = new LinkedList();
        _statementsInfo = new Vector();

        _statements.add(node);
        _statementsInfo.add(new StatementInfo(this, node));
    }

    ///////////////////////////////////////////////////////////////////
    ////                        public methods                     ////

    public void addLabel(String label) {
        StatementInfo info = (StatementInfo)
            _statementsInfo.get(_statementsInfo.size() - 1);
        info.setLabel(label);
    }

    public void addLabel(String label, int index) {
        StatementInfo info = (StatementInfo) _statementsInfo.get(index);
        info.setLabel(label);
    }

    public String getLabel(int index) {
        StatementInfo info = (StatementInfo) _statementsInfo.get(index);
        return info.getLabel();
    }

    public void removeLabel(int index) {
        StatementInfo info = (StatementInfo) _statementsInfo.get(index);
        info.setLabel(null);
    }

    public void addStatement(AST stat) {
        _statements.add(stat);
        _statementsInfo.add(new StatementInfo(this, stat));
    }

    public void addStatement(AST stat, int index) {
        _statements.add(index, stat);
        _statementsInfo.add(new StatementInfo(this, stat));
    }

    public void removeStatement(int index) {
        _statements.remove(index);
        if(index == 0) {
            if(_statements.size() > 0) {
                _absTreeNode = (AST) _statements.get(0);
            }
            else {
                _absTreeNode = null;
            }
        }
        _statementsInfo.remove(index);
    }

    public Iterator statementsIterator() {
        return _statements.iterator();
    }

    public AST getStatement(int index) {
        if(index < 0 || index > _statements.size()) return null;
        return (AST) _statements.get(index);
    }

    public int statementsSize() {
        return _statements.size();
    }

    public String toString() {
        StringBuffer buf = new StringBuffer();
        for(int i = 0;i < _statements.size();i++) {
            AST stat = (AST) _statements.get(i);
            buf.append("(" + i + "): " + stat.toStringTree() + "\n");
        }
        return buf.toString();
    }

    public AST getAbsTreeNode() {
        if(_statements.size() == 0) return null;
        return (AST) _statements.get(0);
    }

    public StatementInfo getStatementInfo(int index) {
        return (StatementInfo) _statementsInfo.get(index);
    }

    public void setStatementInfo(StatementInfo info, int index) {
        _statementsInfo.set(index, info);
    }

    ///////////////////////////////////////////////////////////////////
    ////                      private variables                    ////

    private LinkedList _statements;
    private Vector _statementsInfo;
}
